package com.hptpd.questionnairesurvey.controller.admin;

import com.hptpd.questionnairesurvey.component.Result;
import com.hptpd.questionnairesurvey.vo.admin.LoginForm;
import com.wf.captcha.utils.CaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 彭诗杰
 * @createTime 2020-02-10 17:47
 * @description 管理员controller
 */
@RestController
@RequestMapping("/api/admin")
@Slf4j
public class AdminController {

    @Value("${admin.name}")
    private String adminName;
    @Value("${admin.password}")
    private String adminPwd;

    /**
     * 获取验证码
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @throws Exception Exception
     */
    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result adminLogin(@RequestBody LoginForm loginForm, HttpSession session, HttpServletRequest request) {

        log.debug("传入到phone参数：[{}]  password参数：[{}]  verCode：[{}]",
                loginForm.getPhone(), loginForm.getPassword(), loginForm.getVerCode());
        //验证码判断
        if (!CaptchaUtil.ver(loginForm.getVerCode(), request)) {
            // 清除session中的验证码
            CaptchaUtil.clear(request);
            return Result.fail("验证码不正确");
        } else {
            CaptchaUtil.clear(request);
        }
        if (adminName.equals(loginForm.getPhone()) && adminPwd.equals(loginForm.getPassword())) {
            return Result.ok();
        }
        return Result.fail("账号或密码不正确");
    }

//    @RequestMapping(value = "/logout", method = RequestMethod.GET)
//    public Result adminLogout(String userName, HttpSession session) {
//        return adminService.adminLogout(userName, session);
//    }

}
